﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZhuXingFamilys.Database;
using System.Collections;
using MaterialDesignThemes.Wpf;
using MaterialDesignColors;
using System.Data.SqlClient;
using Autodesk.Revit.UI;


namespace ZhuXingFamilys.User
{
    /// <summary>
    /// forget.xaml 的交互逻辑
    /// </summary>
    public partial class forget : Window
    {
        public forget()
        {
            ColorZoneAssist.SetMode(new GroupBox(), ColorZoneMode.Light);
            Hue hue = new Hue("name", System.Windows.Media.Color.FromArgb(1, 2, 3, 4), System.Windows.Media.Color.FromArgb(1, 5, 6, 7));
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            InitializeComponent();


        }



        private void MoveWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }
        private void ReClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public string UserName;
        public string UserPassword;
        public int border = 1;
        public static Hashtable userall_harsh1;


        private static string SqlServerConnString = Properties.Resource.DBConnect;
        SqlConnection conn = new SqlConnection(SqlServerConnString);


        private void confirm_Button(object sender, RoutedEventArgs e)
        {
            SqlserverDatabase sqlserver = new SqlserverDatabase();
            userall_harsh1 = sqlserver.sqlserver_read();

            if (userall_harsh1 == null)
            {
                MessageBox.Show("无此账户，请先注册!");
                return;
            }
            else
            {
                IDictionaryEnumerator myEnumerator = userall_harsh1.GetEnumerator();  //读取harshtable中的key和value值
                while (myEnumerator.MoveNext()) //将枚举数推到集合的下一元素，若为空，则退出循环
                {

                    UserName = myEnumerator.Key.ToString();         //key值赋给UserName
                    UserPassword = myEnumerator.Value.ToString();    //value值赋给UserPassword

                    string u = Re_Account.Text.ToString();
                    string p = Re_Password.Password.ToString();
                    string p1 = Re_PasswordAgain.Password.ToString();
                    string p2 = Re_PasswordTwoAgain.Password.ToString();

                    if (u == UserName && p == UserPassword && p1 == p2)
                    {

                        //MessageBox.Show("测试");


                        //string updatestring = "update t_User set Password ='p' where UserName = 'u'";
                        string updatestring = "update t_User set Password ='" + p1 + "',CreateTime = GETDATE() where UserName = '" + u + "'";
                        //string updatestring = "insert into t_User values('ddd','a',GETDATE() )";

                        SqlCommand sqlforget = new SqlCommand(updatestring, conn);
                        conn.Open();
                        //sqlInsert.ExecuteNonQuery();    //插入数据
                        int intres = -1;
                        intres = sqlforget.ExecuteNonQuery();
                        if (intres > 0)

                        {

                            MessageBox.Show("修改成功");

                        }

                        else

                        {

                            MessageBox.Show("修改失败");

                        }


                    }
                    else if (border <= userall_harsh1.Count - 1)      //给循环一边界，若循环到所存数据最后一个数仍然不正确，则执行else语句
                    {
                        border++;
                    }
                    else
                        MessageBox.Show("账号或密码错误或修改密码不一致，请重试！");
                }
            }



        }

        private void Re_Account_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }
}

